package com.atguigu.mapper.impl;

import com.atguigu.mapper.EmpMapper;
import com.atguigu.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: liuaojun
 * @CreateTime: 2025-01-18
 * @Description:
 * @Version: 1.0
 */
@Repository
public class EmpMapperImpl implements EmpMapper {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void add(Employee employee) {
        String sql="insert into t_emp (ename,salary,address,did) values(?,?,?,?)";
        jdbcTemplate.update(sql,employee.getEname(),employee.getSalary(),employee.getAddress(),employee.getDid());
    }

    @Override
    public void update(Employee employee) {
        String sql="update t_emp set ename=?,salary=?,address=?,did=? where eid=?";
        jdbcTemplate.update(sql,employee.getEname(),employee.getSalary(),employee.getAddress(),employee.getDid(),employee.getEid());
    }

    @Override
    public void delete(Integer id) {
        String sql="delete from t_emp where eid=?";
        jdbcTemplate.update(sql,id);
    }

    @Override
    public Employee SelectEmpById(Integer id) {
        String sql="select * from t_emp where eid=?";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Employee>(Employee.class),id);
    }

    @Override
    public List<Employee> SelectAllEmp() {
        String sql="select * from t_emp";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Employee.class));
    }
}
